/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ibmmodel3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 *
 * @author mwh
 */
public class VocabularyTableMem implements VocabularyTable {

    final Map<Integer, String> idToWord;

    final Map<String, Integer> wordToId;


    public VocabularyTableMem(String filename) throws FileNotFoundException, IOException {

        idToWord = new HashMap<Integer, String>();
        wordToId = new HashMap<String, Integer>();

        BufferedReader input = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = input.readLine()) != null) {

            String[] tokens = line.split("\t");
            Integer id = Integer.parseInt(tokens[0]);
            String word = tokens[1];
            idToWord.put(id, word);
            wordToId.put(word, id);
        }
    }


    public String get(int id) {
        if (idToWord.containsKey(id)) {
            return idToWord.get(id);
        }
        return "UNK";
    }


    public int get(String word) {
        if (wordToId.containsKey(word)) {
            return wordToId.get(word);
        }
        return 1; // Which is the ID of UNK
    }


    public Iterator<String> keyIterator() {
        return wordToId.keySet().iterator();
    }
}
